//
//  LKTabBar.swift
//  LKWeibo
//
//  Created by 罗锟 on 16/3/2.
//  Copyright © 2016年 andy. All rights reserved.
//

import UIKit

class LKTabBar: UITabBar {

    //重写init方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //添加子控件
        setUpButton()
    }
    
    //防止通过Xib/Sb创建tabbar而报错
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加子控件的方法
    private func setUpButton(){

    addSubview(composeButton)
    
    }
    
    //布局子控件
    override func layoutSubviews() {
        //不设置分类会背景透明
        super.layoutSubviews()
        //设置自定义按钮的位置
        composeButton.center=CGPoint(x: self.center.x, y: self.frame.height*0.5)
        
        //取出对应的按钮
        let width = self.frame.width/5
        
        var index = 0
        
        //遍历按钮
        for value in subviews{
        
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            //计算按钮的X和Width
               let x = CGFloat(index) * width
                
               value.frame.origin.x = x
            
               value.frame.size.width=width
            //设置
                index++
            
                if index==2 {
            
                    index++
            
                }
            }
        }
    }
    
    //   懒加载控件
    private lazy var composeButton: UIButton = {
    
        let button = UIButton()
        //设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        //设置按钮的图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        //设置按钮的大小
        button.sizeToFit()
        
        return button
    }()
}
